package com.rongc.client.freight.base;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.app.ActionBar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.android.volley.Response;
import com.android.volley.error.VolleyError;
import com.rongc.client.core.network.code.HandleCode;
import com.rongc.client.core.network.http.RequestManager;
import com.rongc.client.core.utils.LogUtils;
import com.rongc.client.freight.R;
import com.rongc.client.freight.UniApplication;
import com.rongc.client.freight.base.request.api.IsAuthApi;

import org.json.JSONObject;

import butterknife.ButterKnife;

public abstract class BaseFragment extends Fragment{
    public View baseview;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        if (baseview == null) {
            baseview = inflater.inflate(getLayoutId(), container, false);
        }
        ButterKnife.bind(this, baseview);
        return baseview;
    }

    private boolean isShow = true;
    public boolean isShow(){
        return isShow;
    }
    @Override
    public void onResume() {
        super.onResume();
        isShow = true;
        //TODO now visible to user
    }
    @Override
    public void onPause() {
        super.onPause();
        isShow = false;
        //TODO now invisible to user
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        initView();
        initData();
        initListener();
    }

    public View findViewById(int id){
    	return baseview.findViewById(id);
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
    }
    public abstract void initView();
    public abstract void initData();
    public abstract void initListener();
    public abstract int getLayoutId();


    // 初始化toolbar
    public Toolbar initToolbar(int titleString) {
        AppCompatActivity mAppCompatActivity = (AppCompatActivity) getActivity();
        Toolbar toolbar = (Toolbar) mAppCompatActivity.findViewById(R.id.fragment_toolbar);
        TextView textView = (TextView) toolbar.findViewById(R.id.toolbar_title);
        textView.setText(titleString);
        mAppCompatActivity.setSupportActionBar(toolbar);
        ActionBar actionBar = mAppCompatActivity.getSupportActionBar();
        if (actionBar != null) {
            actionBar.setDisplayHomeAsUpEnabled(false);
            actionBar.setDisplayShowTitleEnabled(false);
        }
        return toolbar;
    }

    // 初始化toolbar
    public Toolbar initToolbar(String titleString) {
        AppCompatActivity mAppCompatActivity = (AppCompatActivity) getActivity();
        Toolbar toolbar = (Toolbar) mAppCompatActivity.findViewById(R.id.fragment_toolbar);
        TextView textView = (TextView) toolbar.findViewById(R.id.toolbar_title);
        textView.setText(titleString);
        mAppCompatActivity.setSupportActionBar(toolbar);
        ActionBar actionBar = mAppCompatActivity.getSupportActionBar();
        if (actionBar != null) {
            actionBar.setDisplayHomeAsUpEnabled(false);
            actionBar.setDisplayShowTitleEnabled(false);
        }
        return toolbar;
    }

    // 初始化toolbar
    public Toolbar initToolbar(boolean hasUP) {
        AppCompatActivity mAppCompatActivity = (AppCompatActivity) getActivity();
        Toolbar toolbar = (Toolbar) mAppCompatActivity.findViewById(R.id.fragment_toolbar);
        mAppCompatActivity.setSupportActionBar(toolbar);
        ActionBar actionBar = mAppCompatActivity.getSupportActionBar();
        if (actionBar != null) {
            actionBar.setDisplayHomeAsUpEnabled(hasUP);
            actionBar.setDisplayShowTitleEnabled(hasUP);

        }
        return toolbar;
    }


    Response.Listener<JSONObject> respAuthTipListener = new Response.Listener<JSONObject>() {

        @Override
        public void onResponse(JSONObject response) {
            if (HandleCode.requestSuccess()) {
                if(response != null ){
                    UniApplication.getInstance().getUserInfo().setAuthStatus(response.optString("status"));
                    UniApplication.getInstance().getUserInfo().save();
                    onAuth();
                }
            }
        }
    };


    Response.ErrorListener errorTipListener = new Response.ErrorListener() {

        @Override
        public void onErrorResponse(VolleyError error) {
            LogUtils.i("error.getMessage(): " + error.getMessage());
        }
    };

    public void onAuth(){

    }

    public void doAuth(){
        RequestManager.getInstance().call(new IsAuthApi(new IsAuthApi.IsAuthParams(),
                respAuthTipListener, errorTipListener));
    }
}
